package com.gopay.bis.manager.bank.expay;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtil;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.bis.dao.payconfig.BisBankExtConfigDAO;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.UmPayConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.umpay.mtpay.UmPayMtPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.ExPayService;

public class UmPayMtPayManager implements BankManagerRemoteService{
	@Resource(name="bisBankExtConfigDAO")
	private BisBankExtConfigDAO bisBankExtConfigDAO;
	
	@Resource(name = "umPayMtPayServiceClient")
	private ExPayService umPayMtPayServiceClient;
	
	@Resource(name = "umPayConfig")
	private Map<String, String> umPayConfig;
	public Object sign(BaseMessage msg) throws GopayException {
		// 获取银行接口通道配置信息
//		BaseBisBankExtConfig bankConfig = bisBankExtConfigDAO
//				.findExtConfigByBankCodeAndBankBusProd(msg.getBankBusProd(),
//						msg.getBank());
		UmPayMtPayMessage message = (UmPayMtPayMessage) msg;
		// 自动路由修改地址参数从配置文件中获取 add by zhengxue
//		message.setNotifyUrl(bankConfig.getPayNotifyUrl());
//		message.setRet_url(bankConfig.getPayRuturnUrl());
//		message.setPayUrl(bankConfig.getPayUrl());
		message =buildUMPMsg(message);
		BankLogUtils.printMtPayManagerSignSendLog(message);
		
		UmPayMtPayMessage returnMessage = (UmPayMtPayMessage) umPayMtPayServiceClient.payment(message);
		message.setTradeState(returnMessage.getTradeState());
		message.setRetMsg(returnMessage.getRetMsg());
		message.setRetCode(returnMessage.getRetCode());
		BankLogUtils.printMtPayManagerSignReturnLog(message);
		message.setAmt(TransUtils.amountConvert2Yuan(message.getAmount()
				.toString()));
		return message;
	}

	public Object verify(BaseMessage msg) throws GopayException {
		UmPayMtPayMessage message = new UmPayMtPayMessage();
		BeanUtils.copyProperties(msg, message);
		BankLogUtils.printExPayManagerVerifySendLog(msg);
		UmPayMtPayMessage umpMessage = (UmPayMtPayMessage) umPayMtPayServiceClient
				.verify(message);
		BankLogUtils.printExPayManagerVerifyReturnLog(message, true);
		return umpMessage;
	}
	
	public Object query(Object obj) throws GopayException {
		UmPayMtPayMessage message =  (UmPayMtPayMessage) obj;
		UmPayMtPayMessage umpayMessage = buildQueryMsg(message);
		BankLogUtils.printExPayManagerQuerySendLog(umpayMessage);
		
		UmPayMtPayMessage umpMessage = (UmPayMtPayMessage) umPayMtPayServiceClient
				.queryOrder(umpayMessage);
		BankLogUtils.printExPayManagerQueryReturnLog(umpMessage);
		return umpMessage;
	}
	/**
	 * 拼装订单查询请求对象
	 * 
	 * @param base
	 * @return
	 */
	public UmPayMtPayMessage buildQueryMsg(UmPayMtPayMessage base) {
		UmPayMtPayMessage umpayMsg = new UmPayMtPayMessage();
		BeanUtils.copyProperties(base, umpayMsg);
		// 协议参数
		umpayMsg.setService(UmPayConstants.UMPAY_QUERY);
		// 在所有的请求方式，响应方式中，sign_type字段均不参与签名验签
		umpayMsg.setSignType((String) umPayConfig.get("UMPAY.SIGNTYPE"));
		umpayMsg.setCharset((String) umPayConfig.get("UMPAY.CHARSET"));
		umpayMsg.setMerId((String) umPayConfig.get("UMPAY.MERID"));
		umpayMsg.setVersion((String) umPayConfig.get("UMPAY.VERSION"));
		
		return umpayMsg;
	}
	/**
	 * 拼装签名对象
	 * 
	 * @param base
	 * @return
	 */
	public UmPayMtPayMessage buildUMPMsg(UmPayMtPayMessage umpayMtMsg) {
		umpayMtMsg.setIdentityType("IDENTITY_CARD"); // 平台只支持身份证
//		if (BankConstants.BANK_LOGO_CMB.equals(umpayMtMsg.getBank())) {
//			// 招商银行信用卡，不能传输证件类型、证件号
//			umpayMtMsg.setIdentityType("");
//			umpayMtMsg.setIdentityCode("");
//		}

		// 协议参数
		umpayMtMsg.setService(UmPayConstants.UMPAY_EXPAY);
		umpayMtMsg.setCharset((String) umPayConfig.get("UMPAY.CHARSET"));
		umpayMtMsg.setMerId((String) umPayConfig.get("UMPAY.MERID"));
		// 在所有的请求方式，响应方式中，sign_type字段均不参与签名验签
		umpayMtMsg.setSignType((String) umPayConfig.get("UMPAY.SIGNTYPE"));
		umpayMtMsg.setVersion((String) umPayConfig.get("UMPAY.VERSION"));
		

		// 业务参数
		umpayMtMsg.setMediaType((String) umPayConfig.get("UMPAY.MEDIATYPE"));
		umpayMtMsg.setMerDate(umpayMtMsg.getOrderDate().substring(0, 8));
		// 如果是人民币，则以分为单位
		umpayMtMsg.setAmt(TransUtils.amountConvert2Fen(umpayMtMsg.getAmount()
				.toString()));
		umpayMtMsg.setAmtType((String) umPayConfig.get("UMPAY.AMTTYPE"));
		umpayMtMsg.setPayType((String) umPayConfig.get("UMPAYMTPAY.PAYTYPE"));
		umpayMtMsg.setMerPriv((String) umPayConfig.get("UMPAY.MERPRIV"));
		String availableDate = umpayMtMsg.getAvailableDate(); 
		umpayMtMsg.setAvailableDate(availableDate.substring(2,4)+availableDate.substring(0,2));
		if(StringUtils.isBlank(umpayMtMsg.getPayUrl())){
			umpayMtMsg.setPayUrl((String) umPayConfig.get("UMPAYMTPAY.PAYURL"));
		}
		if(StringUtils.isBlank(umpayMtMsg.getNotifyUrl())){
			umpayMtMsg.setNotifyUrl((String) umPayConfig.get("UMPAYMTPAY.NOTIFYURL"));
		}
		if(StringUtils.isBlank(umpayMtMsg.getRet_url())){
			umpayMtMsg.setRet_url((String) umPayConfig.get("UMPAYMTPAY.RETURL"));
		}
		return umpayMtMsg;
	}


	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
